package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.constant.TimeConstants;
import com.blankj.utilcode.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityPortScanBinding;
import stark.common.basic.device.NetUtil;
import stark.common.basic.inf.IRetCallback;

public class PortScanActivity extends BaseAc<ActivityPortScanBinding> {

    private boolean isQuery = false;

    @Override
    protected int onCreate() {
        return R.layout.activity_port_scan;
    }

    @Override
    protected void initView() {
        mDataBinding.ivPortScanBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvStartScan.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        if (isQuery) return;
        switch (v.getId()) {
            case R.id.tvStartScan://扫描
                String domain = mDataBinding.etDomain.getText().toString().trim();
                if (TextUtils.isEmpty(domain)) {
                    ToastUtils.showShort(R.string.please_input_domain_ip);
                    return;
                }
                String portStr = mDataBinding.etPort.getText().toString().trim();
                String[] portSubStrs = portStr.split(",");
                if (portSubStrs == null || portSubStrs.length == 0) {
                    ToastUtils.showShort(R.string.please_input_port);
                    return;
                }
                startQuery(domain, portSubStrs);
                break;
        }
    }

    @Override
    protected void initData() {

    }

    //开始查询
    private void startQuery(String domain, String[] portSubStrs) {
        isQuery = true;
        List<Integer> portList = new ArrayList<>();
        for (String portSub : portSubStrs) {
            try {
                portList.add(Integer.parseInt(portSub));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        showDialog(getString(R.string.query_ing));
        long startTime = System.currentTimeMillis();
        NetUtil.portTest(domain, portList, new IRetCallback<List<Integer>>() {
            @Override
            public void onResult(List<Integer> result) {
                StringBuilder builder = new StringBuilder();
                builder.append("PortScanning IP: " + domain + "\n\n");
                for (Integer integer : result) {
                    builder.append("Open : " + integer + "\n\n");
                }
                builder.append("Open Ports : " + result.size() + "\n\n");
                builder.append("Time Taken : " + (System.currentTimeMillis() - startTime) * 1f / TimeConstants.SEC + " second");
                mDataBinding.tvResult.setText(builder.toString());
                isQuery = false;
                dismissDialog();
                mDataBinding.llResult.setVisibility(View.VISIBLE);
            }
        });
    }

}